package com.tfxy.hxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfxy.hxl.R;
import com.tfxy.hxl.entity.Venue;
import com.tfxy.hxl.service.VenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/venue")
public class VenueController {

    @Autowired
    private VenueService venueService;

    // 创建场馆
    @PostMapping("/create")
    public R createVenue(@RequestBody Venue venue) {
        venue.setCreateTime(new Date());
        venue.setUpdateTime(new Date());
        venue.setStatus(1); // 默认启用
        boolean saved = venueService.save(venue);
        return saved ? R.ok().message("场馆创建成功") : R.error().message("场馆创建失败");
    }

    // 分页查询场馆列表
    @GetMapping("/list")
    public R getVenueList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String address) {

        Page<Venue> page = new Page<>(current, size);
        QueryWrapper<Venue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name);
        }

        if (address != null && !address.trim().isEmpty()) {
            queryWrapper.like("address", address);
        }

        queryWrapper.orderByDesc("create_time");
        Page<Venue> result = venueService.page(page, queryWrapper);

        return R.ok()
                .data("venues", result.getRecords())
                .data("total", result.getTotal())
                .data("current", result.getCurrent())
                .data("size", result.getSize());
    }

    // 根据ID查询场馆详情
    @GetMapping("/{id}")
    public R getVenueById(@PathVariable Long id) {
        Venue venue = venueService.getById(id);
        return venue != null ? R.ok().data("venue", venue) : R.error().message("场馆不存在");
    }

    // 更新场馆信息
    @PutMapping("/update")
    public R updateVenue(@RequestBody Venue venue) {
        venue.setUpdateTime(new Date());
        boolean updated = venueService.updateById(venue);
        return updated ? R.ok().message("场馆更新成功") : R.error().message("场馆更新失败");
    }

    // 删除场馆
    @DeleteMapping("/{id}")
    public R deleteVenue(@PathVariable Long id) {
        boolean removed = venueService.removeById(id);
        return removed ? R.ok().message("场馆删除成功") : R.error().message("场馆删除失败");
    }

    // 停用/启用场馆
    @PutMapping("/toggle-status/{id}")
    public R toggleVenueStatus(@PathVariable Long id) {
        Venue venue = venueService.getById(id);
        if (venue == null) {
            return R.error().message("场馆不存在");
        }
        venue.setStatus(venue.getStatus() == 1 ? 0 : 1);
        boolean updated = venueService.updateById(venue);
        return updated ? R.ok().message("状态更新成功") : R.error().message("状态更新失败");
    }
}
